package com.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shop.mapper.Pms_Attr_AttrGroup_Relation_Mapper;
import com.shop.mapper.Pms_Attr_Group_Mapper;
import com.shop.mapper.Pms_Attr_Mapper;
import com.shop.pojo.pms.Pms_Attr_Attrgroup_Relation;
import com.shop.pojo.pms.Pms_Attr_Group;
import com.shop.service.Pms_AttrOrAttrGroup_Service;
import com.shop.service.Pms_Attr_Group_Service;
import com.shop.service.Pms_Attr_Service;
import com.shop.utils.R;
import com.shop.utils.RedisUtil;
import com.shop.vo.Attr;
import com.shop.vo.AttrByattr_Group;
import com.shop.vo.SpuItemAttrGroupVo;
import com.shop.vo.skuGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class Pms_AttrOrAttrGroup_ServiceImpl implements Pms_AttrOrAttrGroup_Service {
    @Autowired
    private Pms_Attr_AttrGroup_Relation_Mapper pms_attr_attrGroup_relation_mapper;
@Autowired
    private Pms_Attr_Group_Service pms_attr_group_service;
@Autowired
private Pms_Attr_Service pms_attr_service;
    @Autowired
    private RedisUtil redisUtil;

    /*
     * @Description 根据分类id和spuid来连表查询数据,然后转换数据
     * @param catalogId
     * @param spuId
     * @return java.util.List<com.shop.vo.SpuItemAttrGroupVo>
     * @author liusupeng
     * @date 2020/10/28 19:52
     */
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long catalogId, Long spuId) {
        List<skuGroupVo> attrGroupWithAttrsBySpuId = pms_attr_attrGroup_relation_mapper.getAttrGroupWithAttrsBySpuId(catalogId, spuId);
        List<SpuItemAttrGroupVo> collect = attrGroupWithAttrsBySpuId.stream().map(item -> {
            SpuItemAttrGroupVo spuItemAttrGroupVo = new SpuItemAttrGroupVo();
            spuItemAttrGroupVo.setGroupName(item.getAttr_group_name());

            String[] attrId = item.getAttr_id().split(",");
            String[] names = item.getAttr_name().split(",");
            String[] values = item.getAttr_value().split(",");
            List<Attr> listattr = new ArrayList<>();
            for (int i = 0; i < attrId.length; i++) {
                Attr attr = new Attr();
                attr.setAttr_id(Long.parseLong(attrId[i]));
                attr.setAttr_name(names[i]);
                attr.setAttr_value(values[i]);
                listattr.add(attr);
            }
            spuItemAttrGroupVo.setAttrs(listattr);
            return spuItemAttrGroupVo;
        }).collect(Collectors.toList());

        return collect;
    }

    /*
     * @Description     根据分类ID和分组id来查询当前分类下的分组的全部属性
     * @param catelogId
     * @return com.shop.utils.R
     * @author liusupeng
     * @date 2020/10/28 19:55
     */
    @Override
    public R findAllAttrGroupByCatalogId(Long catelogId,Long attr_group_id) {
        R r1=new R();
        QueryWrapper<Pms_Attr_Attrgroup_Relation> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("catelog_id",catelogId).eq("attr_group_id",attr_group_id);

        r1.data(pms_attr_attrGroup_relation_mapper.selectList(queryWrapper));
        return r1;
    }
    /*
     * @Description 添加属性分组中间表、跟属性ID和分组ID和分类ID
     * 1、首先根据前台传来的一个属性ID的数组来通过split进行解析
     * 2、然后拿到这个数组里的id然后配合我们的分组id和分类id然后传入到我们的属性分组中间表实体类里面
     * 3、如果在插入的时候出现了异常的话直接return一个500
     * 4、正常执行下去的话就是执行return一个code为200的
     * @param pms_attr_attrgroup_relation
     * @return com.shop.utils.R
     * @author liusupeng
     * @date 2020/10/28 22:06
     */
    @Override
    public R addAttrGroupRelation(String AttrIdStr,Long catelogId,Long attr_group_id) {
        Pms_Attr_Attrgroup_Relation pms_attr_attrgroup_relation=new Pms_Attr_Attrgroup_Relation();
        String[] split = AttrIdStr.split(",");
        for(int i=0;i<split.length;i++){

            pms_attr_attrgroup_relation.setAttr_id(Long.valueOf(split[i]));
            pms_attr_attrgroup_relation.setAttr_group_id(attr_group_id);
            pms_attr_attrgroup_relation.setCatelog_id(catelogId);
            int insert = pms_attr_attrGroup_relation_mapper.insert(pms_attr_attrgroup_relation);
            if(insert<=0){
                return R.error(500,"数据库异常");
            }
        }

        return R.success();
    }
    /*
     * @Description 删除属性分组中间表里的数据
     * 1、首先根据前台传来的一个属性ID的数组来通过split进行解析
     * 2、然后拿到这个数组里的attrid然后配合我们的分组id和分类id来进行查询当前属性分组表里的数据取到主键ID
     * 3、根据ID来进行删除
     * 4、正常执行下去的话就是执行return一个code为200的
     * 5、异常情况直接return一个code为500的异常信息
     * @param pms_attr_attrgroup_relation
     * @return com.shop.utils.R
     * @author liusupeng
     * @date 2020/10/28 22:06
     */
    @Override
    public R delAttrGroupRelation(String AttrIdStr, Long catelogId, Long attr_group_id) {

        String[] split = AttrIdStr.split(",");
        for(int i=0;i<split.length;i++){
            QueryWrapper<Pms_Attr_Attrgroup_Relation> queryWrapper=new QueryWrapper();
            queryWrapper.eq("attr_id",Integer.valueOf(split[i])).eq("attr_group_id",attr_group_id).eq("catelog_id",catelogId);
            Pms_Attr_Attrgroup_Relation pms_attr_attrgroup_relation1 = pms_attr_attrGroup_relation_mapper.selectOne(queryWrapper);
            int insert = pms_attr_attrGroup_relation_mapper.deleteById(pms_attr_attrgroup_relation1.getId());
            if(insert<=0){
                return R.error(500,"数据库异常");
            }
        }

        return R.success();
    }
    /*
     * @Description  查询当前分类下的全部分组，然后再通过分组来查询当前分组下的全部属性，最终封装成对象
     * @param catelogId
     * @return com.shop.utils.R
     * @author liusupeng
     * @date 2020/11/1 14:55
     */
    @Override
    public R findAllAttrByAttr_Group(Long catelogId) {
        List<Pms_Attr_Group> allGroupByCatelogId = pms_attr_group_service.findAllGroupByCatelogId(catelogId);

        List<AttrByattr_Group> collect = allGroupByCatelogId.stream().map((x) -> {
            AttrByattr_Group attrByattr_group = new AttrByattr_Group();
            attrByattr_group.setAttr_group_name(x.getAttr_group_name());
            attrByattr_group.setAttr_group_id(x.getAttr_group_id());
            attrByattr_group.setAllAttrByGroupIdAndCatelogId(pms_attr_service.findAllAttrByCatelogIdAndGroupId(x.getAttr_group_id(), catelogId));
            return attrByattr_group;
        }).collect(Collectors.toList());
        R r1=new R();
        r1.data(collect);
        return r1;
    }
}
